package com.hangwei.paperless.widget;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;

public class MyScrollView extends HorizontalScrollView {

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyScrollView(Context context) {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Rect outRect = new Rect();
        getDrawingRect(outRect);
        int scrollX = this.getScrollX();
        Log.i("MyScrollView", "onInterceptTouchEvent: childWidth" + checkTotalWidth());
        Log.i("MyScrollView", "onInterceptTouchEvent: outRect.right" + outRect.right);
        int x = 0;
        switch (ev.getAction()) {

            case MotionEvent.ACTION_DOWN:
                Log.i("MyScrollView", "onInterceptTouchEvent: ---000000000000000----");
                x = (int) ev.getRawX();
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("MyScrollView", "onInterceptTouchEvent: ---11111111111111----");
                int dX = (int) (ev.getRawX() - x);
                if (getScrollX() == 0) {
                    if (dX > 0) {
                        Log.e("MyScrollView", "onInterceptTouchEvent: ---3333333333333333----");
                        return false;
                    }
                } else if (checkTotalWidth() == outRect.right) {
                    if (dX < 0) {
                        Log.e("MyScrollView", "onInterceptTouchEvent: ---44444444444444----");
                        return false;
                    }
                }
                break;

            default:
                Log.i("MyScrollView", "onInterceptTouchEvent: ---2222222222222----");
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    //    public void startScrollerTask() {
//        intitPosition = getScrollX();
//        postDelayed(scrollerTask, newCheck);
//        checkTotalWidth();
//    }
//
    private int checkTotalWidth() {
//        if (childWidth > 0) {
//            return;
//        }
        int childWidth = 0;
        for (int i = 0; i < getChildCount(); i++) {
            childWidth += getChildAt(i).getWidth();
        }
        return childWidth;
    }
}
